<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/8
 * Time: 10:25
 */
namespace App\Services;
use App\Models\MessagesSent;
use Illuminate\Support\Facades\DB;
use App\Models\AdminUser;
use Illuminate\Support\Facades\Redis;

class MessagesServices
{
    /**
     * @desc 修改群发消息
     * @param int $id 消息ID
     * @param array $params 消息数据
     * @return boolean
     */
    public static function edit_group_sms($id, $params)
    {
        $where_arr = ['id' => $id, 'status' => 0];
        $fields = ['status'];
        $data_info = MessagesSent::where($where_arr)->first($fields);
        if(empty($data_info)) {
            return false;
        }

        // 更新数据
        $where_arr['status'] = [0];
        $data = array();
        if(isset($params['title']) && !empty($params['title'])) {
            $data = [
                'title' => $params['title'],
                'content' => $params['content'],
                'operator_id' => $params['operator_id'],
                'user_type' => $params['user_type'],
                'updated_at' => time()
            ];
        }

        if(isset($params['status']) && ($params['status'] == 1)) {
            $data['status'] = $params['status'];
        }

        $edit_rs = MessagesSent::where($where_arr)->update($data);
        if(!$edit_rs) {
            return false;
        }

        // 写入队列
        if(isset($params['status'])  && ($params['status'] == 1))  {
            // 调用接口
            Redis::rpush("SentGroupMessages_data", $id);
        }

        return true;
    }
}